package com.yunao.service.report.esou;

import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;

import java.util.concurrent.TimeUnit;

public class Isearch2Cache {

	private static Cache<String, byte[]> cache_transient;

	private static Cache<String, byte[]> cache;

	static void init() {
		cache_transient = CacheBuilder.newBuilder()
				.maximumSize(1000)
				.expireAfterAccess(4, TimeUnit.HOURS)
				.expireAfterWrite(8, TimeUnit.HOURS)
				.build();

		cache = CacheBuilder.newBuilder()
				.maximumSize(10000)
				.expireAfterAccess(5, TimeUnit.MINUTES)
				.expireAfterWrite(5, TimeUnit.MINUTES)
				.build();
	}

	static byte[] get(int mode, String key) {
		switch (mode) {
		case Search.CACHE_MODE_READ:
		case Search.CACHE_MODE_FRESH:
		case Search.CACHE_MODE_UPDATE:
			return cache.getIfPresent(key);

		default:
			return cache_transient.getIfPresent(key);
		}
	}

	static void put(int mode, String key, byte[] data) {
		switch (mode) {
		case Search.CACHE_MODE_READ:
		case Search.CACHE_MODE_FRESH:
		case Search.CACHE_MODE_UPDATE:
			cache.put(key, data);
			break;

		default:
			cache_transient.put(key, data);
			break;
		}
	}

}
